home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-06-19 | 8.9 KB | 260 lines |
- /*
- * DefaultTvEventHandler.java 1.0 12 Jan 1997
- *
- * Copyright (c) 1996 Krumel & Associates, Inc. All Rights Reserved.
- *
- * This software is provided as is. Krumel & Associates shall not be liable
- * for any damages suffered by licensee as a result of using, modifying or
- * distributing this software or its derivatives.
- */
-
- package symantec.itools.db.awt;
-
- import java.awt.*;
-
- public class DefaultTvEventHandler implements TvEventHandler {
- Grid grid;
-
- int rowSelectionBase = -1;
-
- public void setupView(Grid v) {
- grid = v;
- }
-
- public boolean handleCellEvent(Event e, TableCell cell) {
- if (e.id == Grid.CELL_KEY_UP || e.id == Grid.LOST_CELL_FOCUS) {
- return true;
- }
-
- grid.clearAllSelections();
- return true;
- }
-
- public boolean handleColHeadingEvent(Event e, TableCell cell) {
- //remember - rows and columns are measured as 0 relative inside grid
- // so must adjust before calling selection functions
-
- //we don't do anything for col heading events
- return true;
- }
-
- public boolean handleRowHeadingEvent(Event e, TableCell cell) {
-
- //remember - rows and columns are measured as 0 relative inside grid
- // so must adjust before calling selection functions
- if (e.id == Grid.BUTTON_UP_EVENT) {
- boolean leftPressed = (e.modifiers & e.ALT_MASK & e.META_MASK) == 0;
- if (e.shiftDown() && leftPressed) {
- if (grid.isSelectionBaseSet()) {
- //start a selection
- grid.selectRange(cell.row() + 1, 0);
- } else {
- grid.setSelectionBase(cell.row()+1, 0);
- grid.selectRange(cell.row() + 1, 0);
- }
- } else if (e.controlDown() && leftPressed) {
- if (!grid.isRangeSelected()) {
- grid.setSelectionBase(cell.row()+1, 0);
- }
-
- grid.toggleRow(cell.row() + 1);
- } else if (leftPressed) {
- boolean set = grid.isRowSet(cell.row()+1);
- boolean auto = grid.getAutoRedraw();
- grid.setAutoRedraw(false);
- grid.clearAllSelections();
- grid.setSelectionBase(cell.row()+1, 0);
- grid.setRow(cell.row() + 1, !set);
- grid.setAutoRedraw(auto);
- grid.redrawAsync();
- }
- }
-
- return true;
- }
-
- public boolean handleCornerCellEvent(Event e, TableCell cell) {
- //remember - rows and columns are measured as 0 relative inside grid
- // so must adjust before calling selection functions
-
- //we don't do anything for this cell
- return true;
- }
-
- public boolean handleToolbarEvent(Event e) {
- if (e.target instanceof java.awt.TextField && e.id == Event.ACTION_EVENT) {
- java.awt.TextField go = (java.awt.TextField)e.target;
- try {
- String rowText = go.getText();
- if (rowText.equals("")) { return true; }
- int row = Integer.parseInt(rowText);
- grid.gotoRow(row);
- } catch(NumberFormatException ex) {
- handleException(null, ex);
- }
- }
-
- if (!(e.target instanceof Button) ||
- (e.target instanceof Button && e.id != Event.ACTION_EVENT))
- {
- return false;
- }
-
- Button button = (Button)e.target;
- String label = button.getLabel();
-
- if (label.equals(Grid.gotoRowLabel)) {
- try {
- String rowText = (String)e.arg;
- if (rowText.equals("")) { return true; }
- int row = Integer.parseInt(rowText);
- grid.gotoRow(row);
- } catch(NumberFormatException ex) {
- handleException(null, ex);
- }
- } else if (label.equals(Grid.appendRowLabel)) {
- try {
- grid.appendRow();
- } catch(TypeNotSupported ex) {
- handleException(null, ex);
- }
-
- return true;
- } else if (label.equals(Grid.deleteRowLabel)) {
- int vv=1;
- int actrow=1;
-
- try{
- actrow=grid.currSelectedCell.row()+1;
-
- }
- catch(NullPointerException ex){vv=0;}
- int rows[] = grid.getSelectedRows();
- boolean auto = grid.getAutoRedraw();
- grid.setAutoRedraw(false);
-
- try {
- if (rows.length == 0) {
- //this is zero relative so must adjust to call API
- Coordinate c = grid.getCurrentCellCoordinates();
- if (c != null) {
- grid.deleteRow(c.row());
- grid.setFocusToRow(0);
- // grid.redrawAsync();;
- }
- } else {
- for(int i=rows.length-1; i>=0; i--) {
- grid.deleteRow(rows[i]);
- }
- }
- } catch(TypeNotSupported ex) {
- handleException(null, ex);
- }
- if(vv==0){
- grid.currSelectedCell.setRow(0);
- grid.setFocusToRow(0);}
- if(vv!=0){
- grid.setFocusToRow(actrow);}
-
- grid.setAutoRedraw(auto);
- grid.clearAllSelections();
- grid.redraw();
- return true;
- } else if (label.equals(Grid.insertRowLabel)) {
- int row = grid.getFirstSelectedRow();
- try {
- grid.insertRow(row);
- } catch(TypeNotSupported ex) {
- handleException(null, ex);
- }
-
- // grid.clearAllSelections();
- return true;
- } else if (label.equals(Grid.saveLabel)) {
- try {
- grid.save();
- // grid.redrawAsync();
- } catch(TypeNotSupported ex) {
- handleException(null, ex);
- }
-
- return true;
- } else if (label.equals(Grid.restartLabel)) {
- grid.restart();
- // grid.redrawAsync();
- return true;
- } else if (label.equals(Grid.undoRowLabel)) {
- int rows[] = grid.getSelectedRows();
- boolean auto = grid.getAutoRedraw();
- grid.setAutoRedraw(false);
-
- try {
- if (rows.length == 0) {
- //this is zero relative so must adjust to call API
- Coordinate c = grid.getCurrentCellCoordinates();
- if (c != null) {
- grid.undoRow(c.row());
- }
- } else {
- for(int i=rows.length-1; i>=0; i--) {
- grid.undoRow(rows[i]);
- }
- }
- } catch(TypeNotSupported ex) {
- handleException(null, ex);
- }
-
- grid.setAutoRedraw(auto);
- grid.clearAllSelections();
- // grid.redrawAsync();;
- return true;
- } else if (label.equals(Grid.undeleteRowLabel)) {
- int vv=1;
- int actrow=1;
- try{
- actrow=grid.currSelectedCell.row()+1;
- }
- catch(NullPointerException ex){vv=0;}
- int rows[] = grid.getSelectedRows();
- boolean auto = grid.getAutoRedraw();
- grid.setAutoRedraw(false);
-
- try {
- if (rows.length == 0) {
- //this is zero relative so must adjust to call API
- Coordinate c = grid.getCurrentCellCoordinates();
- if (c != null) {
- grid.undeleteRow(c.row());
- // grid.redrawAsync();;
- }
- } else {
- for(int i=rows.length-1; i>=0; i--) {
- grid.undeleteRow(rows[i]);
- }
- }
- } catch(TypeNotSupported ex) {
- handleException(null, ex);
- }
- if(vv==0){
- grid.currSelectedCell.setRow(0);
- grid.setFocusToRow(0);
- grid.requestFocus();}
- if(vv!=0){
- grid.setFocusToRow(actrow);}
- grid.setAutoRedraw(auto);
- grid.clearAllSelections();
- grid.redraw();
- grid.requestFocus();
- return true;
- }
-
- return true;
- }
-
- public void handleException(Coordinate pos, Exception exc) {
- if (pos != null) {
- System.out.println("Error occurred in Grid at row="+pos.row()+" col="+pos.col());
- }
- exc.printStackTrace();
- }
- }